<?php
class Fe_faq_model extends CI_Model {
	
	function __construct()
	{
			parent::__construct();
	}	
	
    
	function _search_faq_list($keyword, $cate_id, $limit_from, $limit_to)
	{
		//prepare data
		$keyword = addslashes($keyword);
		//
		$db = $this->load->database('default', TRUE);
    $a = array();
		$sql = "select count(faq.faq_id) c FROM faq
						inner join faq_cate on faq.faq_cate_id=faq_cate.cate_id
						where 1=1";
		if ($keyword != "")
		{
			$sql.=" and faq.question like N? ";
			$a[] = "%".$keyword."%";
		}
		if ($cate_id != "")
		{
			$sql.=" and faq.faq_cate_id=? ";
			$a[] = $cate_id;
		}            
		$sql.=" and faq.status = '1' order by faq_id DESC;";
		$query = $db->query($sql,$a);
		$aRows = $query->result();
		$iRow_Count = $aRows[0]->c;				

		$a = array();
		$sql = "SELECT faq.*, faq_cate.cate_name from faq
						inner join faq_cate on faq.faq_cate_id=faq_cate.cate_id 
						where 1=1";
		if ($keyword != "")
		{
			$sql.=" and faq.question like N? ";
			$a[] = "%".$keyword."%";
		}
		if ($cate_id != "")
		{
			$sql.=" and faq.faq_cate_id=? ";
			$a[] = $cate_id;
		}        
		$sql.=" and faq.status = '1' order by faq_id DESC LIMIT {$limit_from},{$limit_to};";
		$query = $db->query($sql,$a); 
		$result = $query->result();
		return array($iRow_Count,$result);		
	}		
	
    //dùng cho trang Rao Vặt
	function _get_faq_list_bycate($cate_id, $limit_from, $limit_to)
	{
		$db = $this->load->database('default', TRUE);
		$sql = "SELECT faq.faq_id, faq.url, faq.title, faq.desc, faq.create_date, city.city_name FROM faq
						inner join city on faq.city_id=city.city_id
						WHERE faq_cate_id = ? and status='1'
		 			  ORDER BY faq_id DESC LIMIT {$limit_from},{$limit_to};";
		$query = $db->query($sql,array($cate_id));
		return $query->result();
	}		

	function _get_faq_list($id, $limit_from, $limit_to)
	{
		$db = $this->load->database('default', TRUE);
		$sql = "SELECT faq.faq_id, faq.url, faq.title, faq.create_date, city.city_name FROM faq
						inner join city on faq.city_id=city.city_id
						WHERE faq_id <> {$id}
		 			  ORDER BY faq_id DESC LIMIT {$limit_from},{$limit_to};";
		$query = $db->query($sql);
		return $query->result();
	}
		
	function _get_faq_nopaging($limit_start, $limit_count)
	{
		$db = $this->load->database('default', TRUE);
		$sql = "SELECT url, faq_id, title from faq where status='1' order by faq_id desc LIMIT ?,?;";
		$query = $db->query($sql, array($limit_start, $limit_count));
		//echo $sql;
		return array($query->result());			
	}					
	
	function _get_faq_cate_list()
	{
		$db = $this->load->database('default', TRUE);
		$sql = "SELECT * FROM faq_cate";
		$query = $db->query($sql);
		return $query->result();
	}			
	
	function _get_city_list()
	{
		$db = $this->load->database('default', TRUE);
		$sql = "SELECT * FROM city";
		$query = $db->query($sql);
		return $query->result();
	}			
	
	function _insert_faq($tablename, $data)
	{
		//$sql = $this->db->query("SET NAMES 'utf8'");
		$db = $this->load->database('default', TRUE);
		$db->insert($tablename, $data);
		return $db->insert_id();
	}				
	
	// hàm nay để test nested array
	function do_dump(&$var, $var_name = NULL, $indent = NULL, $reference = NULL)
	{
			$do_dump_indent = "<span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ";
			$reference = $reference.$var_name;
			$keyvar = 'the_do_dump_recursion_protection_scheme'; $keyname = 'referenced_object_name';
	
			if (is_array($var) && isset($var[$keyvar]))
			{
					$real_var = &$var[$keyvar];
					$real_name = &$var[$keyname];
					$type = ucfirst(gettype($real_var));
					echo "$indent$var_name <span style='color:#a2a2a2'>$type</span> = <span style='color:#e87800;'>&amp;$real_name</span><br>";
			}
			else
			{
					$var = array($keyvar => $var, $keyname => $reference);
					$avar = &$var[$keyvar];
		 
					$type = ucfirst(gettype($avar));
					if($type == "String") $type_color = "<span style='color:green'>";
					elseif($type == "Integer") $type_color = "<span style='color:red'>";
					elseif($type == "Double"){ $type_color = "<span style='color:#0099c5'>"; $type = "Float"; }
					elseif($type == "Boolean") $type_color = "<span style='color:#92008d'>";
					elseif($type == "NULL") $type_color = "<span style='color:black'>";
		 
					if(is_array($avar))
					{
							$count = count($avar);
							echo "$indent" . ($var_name ? "$var_name => ":"") . "<span style='color:#a2a2a2'>$type ($count)</span><br>$indent(<br>";
							$keys = array_keys($avar);
							foreach($keys as $name)
							{
									$value = &$avar[$name];
									$this->do_dump($value, "['$name']", $indent.$do_dump_indent, $reference);
							}
							echo "$indent)<br>";
					}
					elseif(is_object($avar))
					{
							echo "$indent$var_name <span style='color:#a2a2a2'>$type</span><br>$indent(<br>";
							foreach($avar as $name=>$value) $this->do_dump($value, "$name", $indent.$do_dump_indent, $reference);
							echo "$indent)<br>";
					}
					elseif(is_int($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color$avar</span><br>";
					elseif(is_string($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color\"$avar\"</span><br>";
					elseif(is_float($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color$avar</span><br>";
					elseif(is_bool($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color".($avar == 1 ? "TRUE":"FALSE")."</span><br>";
					elseif(is_null($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> {$type_color}NULL</span><br>";
					else echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $avar<br>";
	
					$var = $var[$keyvar];
			}
	}				
}
?>